package space.panker916.config;

import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
import space.panker916.quartz.PostScoreRefreshJob;

// 配置 -> 数据库 -> 调用
// 介绍 quartz 的博客：https://www.cnblogs.com/summerday152/p/14192845.html
// 关于 springboot 和 quartz 配置有关的博客 ：https://www.cnblogs.com/summerday152/p/14193968.html
@Configuration
public class QuartzConfig {
    // FactoryBean 可以简化 Bean 的实例化过程
    // 1. 通过 FactoryBean 封装 Bean 的实例化过程
    // 2. 将 FactoryBean 装配到 Spring 里面
    // 3. 将 FactoryBean 注入给其它的 Bean
    // 4. 该 Bean 得到的是 FactoryBean 所管理的对象实例

    // 配置 JobDetail （刷新帖子的任务）
    @Bean
    public JobDetailFactoryBean postScoreRefreshJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(PostScoreRefreshJob.class);
        factoryBean.setName("postScoreRefreshJob");
        factoryBean.setGroup("communityJobGroup");
        factoryBean.setDurability(true);            // 持久化
        factoryBean.setRequestsRecovery(true);      // 请求恢复

        return factoryBean;
    }

    // 配置 Trigger（有 SimpleTriggerFactoryBean, CronTriggerFactoryBean 两种配置方式）
    @Bean
    public SimpleTriggerFactoryBean postScoreRefreshTrigger(JobDetail postScoreRefreshJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(postScoreRefreshJobDetail);
        factoryBean.setName("postScoreRefreshTrigger");
        factoryBean.setGroup("communityTriggerGroup");
        factoryBean.setRepeatInterval(1000 * 60 * 3);
        factoryBean.setJobDataMap(new JobDataMap());

        return factoryBean;
    }

}
